package com.jie.myvhr.service;

import com.jie.myvhr.bean.Employee;
import com.jie.myvhr.bean.Nation;
import com.jie.myvhr.bean.PoliticsStatus;
import com.jie.myvhr.mapper.EmpMapper;
import com.jie.myvhr.mapper.SalaryMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author jie
 * @date 2019/6/30 -15:53
 */
@Service
@Transactional
public class EmpService {
    SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
    SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
    SimpleDateFormat birthdayFormat = new SimpleDateFormat("yyyy-MM-dd");
    DecimalFormat decimalFormat = new DecimalFormat("##.00");

    @Resource
    private EmpMapper empMapper;

    @Resource
    private SalaryMapper salaryMapper;


    public List<Nation> getAllNations() {
        return empMapper.getAllNations();
    }

    public List<PoliticsStatus> getAllPolitics() {
        return empMapper.getAllPolitics();
    }

    public Long getMaxWorkID() {
        Long maxWorkID = empMapper.getMaxWorkID();
        return maxWorkID == null ? 0 : maxWorkID;
    }

    /**
     * 获取员工信息
     */
    public List<Employee> getEmployeeByPage(String keywords, Long politicId, Long nationId, Long posId, Long jobLevelId, String engageForm, Long departmentId, String beginDateScope) {
        Date startBeginDate = null;
        Date endBeginDate = null;
        if (beginDateScope != null && beginDateScope.contains(",")) {
            try {
                String[] split = beginDateScope.split(",");
                startBeginDate = birthdayFormat.parse(split[0]);
                endBeginDate = birthdayFormat.parse(split[1]);
            } catch (ParseException e) {
            }
        }
        return empMapper.getEmployeeByPage(keywords, politicId, nationId, posId, jobLevelId, engageForm, departmentId, startBeginDate, endBeginDate);
    }

    public boolean deleteEmpById(String ids) {
        String[] split = ids.split(",");
        salaryMapper.delEmpSalary(split);
        return empMapper.deleteEmpById(split) == split.length;
    }

    public int updateEmp(Employee employee) {
        Date beginContract = employee.getBeginContract();
        Date endContract = employee.getEndContract();
        Double contractTerm = (Double.parseDouble(yearFormat.format(endContract)) -
                Double.parseDouble(yearFormat.format(beginContract))) * 12 +
                Double.parseDouble(monthFormat.format(endContract)) -
                Double.parseDouble(monthFormat.format(beginContract));
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(contractTerm / 12)));
        return empMapper.updateEmp(employee);
    }

    public int addEmp(Employee employee) {
        Date beginContract = employee.getBeginContract();
        Date endContract = employee.getEndContract();
        Double contractTerm = (Double.parseDouble(yearFormat.format(endContract)) -
                Double.parseDouble(yearFormat.format(beginContract))) * 12 +
                Double.parseDouble(monthFormat.format(endContract)) -
                Double.parseDouble(monthFormat.format(beginContract));
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(contractTerm/12)));
        return empMapper.addEmp(employee);
    }

    /**
     * 获取所有员工信息
     */
    public List<Employee> getAllEmployees() {
        return empMapper.getEmployeeByPage("", null, null, null, null, null, null, null, null);
    }
    /**
     * 添加
     */
    public int addEmps(List<Employee> emps) {
        return empMapper.addEmps(emps);
    }
    /**
     * 查询账套
     */
    public List<Employee> getEmployeeByPageShort(){
        return empMapper.getEmployeeByPageShort();
    }
}
